package cn.itsource.org.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.User;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    RedisService redisService;
    @Autowired
    UserMapper userMapper;
    @Override
    public AjaxResult LoginByUser(User user) {
        String code = redisService.getKeyObjectValue(user.getPhone(), String.class);
        if(Objects.equals(user.getPassword(),user.getConfigPassword())){
            //密码加密
            String password = user.getPassword();
            String salt = RandomUtil.randomString(6);
            String s = MD5.create().setSalt(salt.getBytes()).digestHex(password);
            user.setUsername(user.getPhone());
            user.setSalt(salt);
            user.setPassword(s);
        }else{
            return AjaxResult.setMsg("两次密码不相同");
        }
        if(code.equals(user.getCode())){
            userMapper.insertSelective(user);
            return AjaxResult.me();
        }else {
            return AjaxResult.setMsg("验证码错误");
        }
    }
}
